package com.scs.application.modules.upms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.PageResult;
import com.scs.application.core.mvc.controller.BaseController;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.upms.entity.Application;
import com.scs.application.modules.upms.request.ApplicationQueryRequest;
import com.scs.application.modules.upms.service.ApplicationService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 */
@AllArgsConstructor
@RestController
@RequestMapping("/upms/applications")
public class ApplicationController extends BaseController {

    private ApplicationService applicationService;

    @PostMapping
    public CommonResult add(@RequestBody Application application) {
        boolean bSave = applicationService.saveCascadeById(application);
        return bSave ? CommonResult.ok("新增成功", application) : CommonResult.error("新增失败", application);
    }

    @PutMapping
    public CommonResult update(@RequestBody Application application) {
        boolean bUpdate = applicationService.updateCascadeById(application);

        return bUpdate ? CommonResult.ok("保存成功", application) : CommonResult.error("保存失败", application);
    }

    @GetMapping("/page")
    public PageResult page(ApplicationQueryRequest request) {
        IPage page = applicationService.page(request.getPage(), new QueryWrapper<Application>()
                .like(StringUtils.isNotBlank(request.getName()), "name", request.getName()));

        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/list")
    public CommonResult list(ApplicationQueryRequest request) {
        List<Application> list = applicationService.list(new QueryWrapper<Application>()
                .like(StringUtils.isNotBlank(request.getName()), "name", request.getName()));
        return CommonResult.ok("查询成功", list);
    }


    @DeleteMapping("/{id}")
    public CommonResult deleteById(@PathVariable("id") Long id) {
        boolean bRemove = applicationService.removeCascadeById(id);
        return bRemove ? CommonResult.ok("删除成功") : CommonResult.error("删除失败");
    }

}
